package com.java1234.service.impl;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.java1234.entity.Blog;
import com.java1234.entity.BlogType;
import com.java1234.entity.Blogger;
import com.java1234.entity.Link;
import com.java1234.service.BlogService;
import com.java1234.service.BlogTypeService;
import com.java1234.service.BloggerService;
import com.java1234.service.LinkService;

/**
  * @Project: Blog
  * @Title: InitComponent
  * @Description: 初始化组件 把博主信息 根据博客类别分类信息 根据日期归档分类信息 存放到application中，用以提供页面请求性能
  * @author: zhangxue
  * @date: 2016年12月18日上午11:33:53
  * @company: webyun
  * @Copyright: Copyright (c) 2015
  * @version v1.0
 */
@Component
public class InitComponent implements ServletContextListener,ApplicationContextAware{
	
	private static Logger logger = LoggerFactory.getLogger(InitComponent.class);
	
	private static ApplicationContext applicationContext;
	
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		
		print("------------------------------------------------------------------------------");
		print("--                  欢迎使用个人微博系统                                power by 张雪                        --");
		print("------------------------------------------------------------------------------");
		
		print("正在加载您的信息:");
		print("loading....\t10%");
		//获得ServletContext
		ServletContext application=servletContextEvent.getServletContext();
		
		//1.查询博主信息，并设置为全局变量
		try {
			BloggerService bloggerService=(BloggerService) applicationContext.getBean("bloggerService");
			//查询博主信息，并设置为全局变量
			Blogger blogger=bloggerService.find();
			blogger.setPassword(null);
			application.setAttribute("blogger", blogger);
			print("loading....\t30%,加载您的个人信息成功");
		} catch (BeansException e) {
			e.printStackTrace();
			
		}
		
		//2.查询博客类别
		try {
			BlogTypeService blogTypeService=(BlogTypeService) applicationContext.getBean("blogTypeService");
			List<BlogType> blogTypeCountList=blogTypeService.countList(); 
			application.setAttribute("blogTypeCountList", blogTypeCountList);
			print("loading....\t50%,加载您的博客分类信息成功");
		} catch (BeansException e) {
			e.printStackTrace();
		}
		
		//3.查询博客的数量，根据日期分组查询博客
		try {
			BlogService blogService=(BlogService) applicationContext.getBean("blogService");
			List<Blog> blogCountList=blogService.countList();  
			application.setAttribute("blogCountList", blogCountList);
			print("loading....\t75%,加载全部博客成功");
		} catch (BeansException e) {
			e.printStackTrace();
		}
		
		//4.查询所有的友情链接信息
		try {
			LinkService linkService=(LinkService) applicationContext.getBean("linkService");
			List<Link> linkList=linkService.list(null);
			application.setAttribute("linkList", linkList);
			print("loading....\t100%,加载所有的友情链接信息成功");
		} catch (BeansException e) {
			e.printStackTrace();
		}
	}

	public void contextDestroyed(ServletContextEvent sce) {
		
	}
	
	private static void print(String c){
		System.out.println(c);
	}
}
